package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 华龙网> 解析类
 *
 * @author KK
 *
 */
public class CqnewsNewsAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://[a-z]*.cqnews.net/[a-z]+/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",

				};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		Long date=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);
		String author=null;
		title=doc.select("div#content h1").text();



		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, "-");
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("td#get_title").text();
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#now > h1").text();
		}

		author=StringUtils.substringBetween(htmltxt,"var get_laiyuan='","'");
		if(StringUtils.isNotBlank(author)){
			Document parse=Jsoup.parse(author);
			author=parse.text();
		}

		content =HtmlCleaner.getContentHtml(url, doc.select("div.Article"));
		if (StringUtils.isNullOrEmpty(content)) {
			content =HtmlCleaner.getContentHtml(url, doc.select("div#main_text"));
		}
		if (StringUtils.isNullOrEmpty(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div#now > div#main_text"));
		}

		if (StringUtils.isNullOrEmpty(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.dic_img"));
		}
		if (StringUtils.isNullOrEmpty(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div#main_text"));
		}
		if (StringUtils.isNullOrEmpty(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("td.t01 p"));
		}

		date=JSoupUtils.matchDate(doc, "div.time","div#content","div.Deputy","div#now > p.time","h2");
		if(date==null){
			date=DateUtils.matchDate(url);
		}
			NewsMeta news=new NewsMeta();
			news.setUrl(url);
			news.setType(1);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setAuthor(author);
			return news;
	}



	public static void main(String[] args) {
		CqnewsNewsAnalyse a=new CqnewsNewsAnalyse();
		String url="http://news.cqnews.net/html/2014-08/13/content_31654247.htm";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
